package com.tools.common.container.r;

import com.tools.common.container.OwnPairs;
import com.tools.common.exception.InvalidOperationException;
import com.tools.common.object.Note;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.BiFunction;
import java.util.function.Function;

/**
 * 只读 TreeMap 键值对集合
 * */
@Note("只读 TreeMap 键值对集合")
public final class OnlyReadTreeMap<K, V> extends TreeMap<K, V> implements OnlyReadMap {

    public OnlyReadTreeMap() {
        super();
    }

    public OnlyReadTreeMap(Map<? extends K, ? extends V> map) {
        if(map == null || map.isEmpty()) return;
        super.putAll(map);
    }

    public OnlyReadTreeMap(Map<? extends K, ? extends V> map, Comparator<? super K> c) {
        super(c);
        if(map == null || map.isEmpty()) return;
        super.putAll(map);
    }

    public OnlyReadTreeMap(OwnPairs<? extends K, ? extends V> pair) {
        if(pair == null || pair.isEmpty()) return;
        for (Map.Entry<? extends K, ? extends V> e : pair.entrySet()) {
            super.put(e.getKey(), e.getValue());
        }
    }

    public OnlyReadTreeMap(OwnPairs<? extends K, ? extends V> pair, Comparator<? super K> c) {
        super(c);
        if(pair == null || pair.isEmpty()) return;
        for (Map.Entry<? extends K, ? extends V> e : pair.entrySet()) {
            super.put(e.getKey(), e.getValue());
        }
    }

    @Override
    public V put(K key, V value) {
        throw new InvalidOperationException("该 Map 集合只支持读操作");
    }

    @Nullable
    @Override
    public V putIfAbsent(K key, V value) {
        throw new InvalidOperationException("该 Map 集合只支持读操作");
    }

    @Override
    public void putAll(Map<? extends K, ? extends V> map) {
        throw new InvalidOperationException("该 Map 集合只支持读操作");
    }

    @Override
    public V remove(Object key) {
        throw new InvalidOperationException("该 Map 集合只支持读操作");
    }

    @Override
    public boolean remove(Object key, Object value) {
        throw new InvalidOperationException("该 Map 集合只支持读操作");
    }

    @Override
    public void clear() {
        throw new InvalidOperationException("该 Map 集合只支持读操作");
    }

    @Override
    public Map.Entry<K, V> pollFirstEntry() {
        throw new InvalidOperationException("该 Map 集合只支持读操作");
    }

    @Override
    public Map.Entry<K, V> pollLastEntry() {
        throw new InvalidOperationException("该 Map 集合只支持读操作");
    }

    @Override
    public boolean replace(K key, V oldValue, V newValue) {
        throw new InvalidOperationException("该 Map 集合只支持读操作");
    }

    @Override
    public V replace(K key, V value) {
        throw new InvalidOperationException("该 Map 集合只支持读操作");
    }

    @Override
    public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
        throw new InvalidOperationException("该 Map 集合只支持读操作");
    }

    @Override
    public V computeIfAbsent(K key, @NotNull Function<? super K, ? extends V> mappingFunction) {
        throw new InvalidOperationException("该 Map 集合只支持读操作");
    }

    @Override
    public V computeIfPresent(K key, @NotNull BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
        throw new InvalidOperationException("该 Map 集合只支持读操作");
    }

    @Override
    public V compute(K key, @NotNull BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
        throw new InvalidOperationException("该 Map 集合只支持读操作");
    }

    @Override
    public V merge(K key, @NotNull V value, @NotNull BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
        throw new InvalidOperationException("该 Map 集合只支持读操作");
    }
}
